#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int ntest;

struct per{
	string name;
	int att, def;
} p[10];

int main()
{
	scanf("%d\n", &ntest);

	for (int test = 1; test <= ntest; test++) 
	{
		for (int i = 0; i < 10; i++) cin >> p[i].name >> p[i].att >> p[i].def;

		for (int i = 0; i < 10; i++)
			for (int j = i+1; j < 10; j++)
			{
				if ((p[i].att < p[j].att) || (p[i].att == p[j].att && p[i].def > p[j].def) || (p[i].att == p[j].att && p[i].def == p[j].def && p[i].name > p[j].name))
				{
					swap(p[i], p[j]);
				}
			}

		cout << "Case " << test << ":" << endl;

		for (int i = 0; i < 5; i++)
		for (int j = i+1; j < 5; j++)
			if (p[i].name > p[j].name) swap(p[i], p[j]);

		for (int i = 5; i < 10; i++)
		for (int j = i+1; j < 10; j++)
			if (p[i].name > p[j].name) swap(p[i], p[j]);

		cout << "(" << p[0].name;
		for (int i = 1; i < 5; i++) cout << ", " << p[i].name;
		cout << ")" << endl;
	
		cout << "(" << p[5].name;
		for (int i = 6; i < 10; i++) cout << ", " << p[i].name;
		cout << ")" << endl;
		
	}

	return 0;
}
